<template>
	<view class="wrap">
		<view class="main" v-if="orderData">
			<view class="state-box" :class="stateStyle">
				<view class="iconfont" :class="stateIcon"></view>
				<view class="s-tit">{{orderData.status_text}}</view>
			</view>
			<view class="box">
				<view class="b-row">
					<view class="b-tit">就餐方式</view>
					<view class="b-cont">
						<text class="s-text">{{orderData.enjoy_type_text}}</text>
					</view>
				</view>
				<view class="b-row" v-if="orderData.enjoy_type === 'meat'">
					<view class="b-tit">桌位号</view>
					<view class="b-cont">
						<text class="s-text">{{orderData.desk_data.number}}</text>
					</view>
				</view>
				<view class="b-row" v-else-if="orderData.enjoy_type === 'pack'">
					<view class="b-tit">联系电话 </view>
					<view class="b-cont">
						<text class="s-text">{{orderData.contact}}</text>
					</view>
				</view>
			</view>
			<view class="gift-head">{{orderData.shop_data.name}}</view>
			<view class="gift-list">
				<view class="gift-item" @click="onJumpGoodsDetail(item.goods_type, item.goods_id)"
					v-for="(item, index) in orderData.goods_list" :key="index">
					<view class="b-left">
						<image :src="item.goods_cover" mode="aspectFill"></image>
					</view>
					<view class="b-main">
						<view class="b-title">{{item.goods_name}}</view>
						<view class="b-spec">
							<view class="s-value">{{item.goods_names}}</view>
						</view>
						<view class="b-fund">
							<view class="b-amount">
								<view class="s-count">x{{item.goods_count}}</view>
								<view class="s-worth">¥{{item.goods_price}}</view>
								<view class="s-num">¥{{item.payment}}</view>
							</view>
							<view class="b-action">
								<view class="s-btn f-apply" @click.stop="onJumpRefund(item.order_item_no)"
									v-if="item.opt_actions.indexOf('can_refund') !== -1">
									退款</view>
								<view class="s-btn f-result" @click.stop="onJumpRefund(item.order_item_no)"
									v-if="item.opt_actions.indexOf('refund_result') !== -1">退款结果</view>
							</view>
						</view>
					</view>
				</view>
				<view class="gift-note">
					<view class="b-tit">备注信息</view>
					<view class="b-cont">
						<view class="s-text">{{orderData.note}}</view>
					</view>
				</view>
			</view>
			<view class="box">
				<view class="b-row">
					<view class="b-tit">订单编号</view>
					<view class="b-cont">
						<text class="s-text">{{orderData.order_no}}</text>
						<text class="s-copy" @click="onCopy(orderData.order_no)">点击复制</text>
					</view>
				</view>
				<view class="b-row">
					<view class="b-tit">商品总价</view>
					<view class="b-cont">
						<text class="s-text">¥{{orderData.total_price}}</text>
					</view>
				</view>
				<view class="b-row">
					<view class="b-tit">优惠金额</view>
					<view class="b-cont">
						<text class="s-text">¥{{orderData.discount}}</text>
					</view>
				</view>
				<view class="b-row">
					<view class="b-tit">应付金额</view>
					<view class="b-cont">
						<text class="s-text">¥{{orderData.payment}}</text>
					</view>
				</view>
				<view class="b-row" v-if="orderData.balance">
					<view class="b-tit">余额支付</view>
					<view class="b-cont">
						<text class="s-text">¥{{orderData.balance}}</text>
					</view>
				</view>
				<view class="b-row" v-if="!orderData.payment_time">
					<view class="b-tit">现付金额</view>
					<view class="b-cont">
						<text class="s-text">¥{{leftAmount}}</text>
					</view>
				</view>
				<view class="b-row" v-if="orderData.payment_time">
					<view class="b-tit">现付金额</view>
					<view class="b-cont">
						<text class="s-text">¥{{orderData.payment_rel}}</text>
					</view>
				</view>
				<view class="b-row" v-if="orderData.payment_time">
					<view class="b-tit">支付方式</view>
					<view class="b-cont">
						<text class="s-text">{{orderData.payment_name}}</text>
					</view>
				</view>
				<view class="b-row" v-if="orderData.payment_time">
					<view class="b-tit">支付时间</view>
					<view class="b-cont">
						<text class="s-text">{{timeFormat(orderData.payment_time)}}</text>
					</view>
				</view>
				<view class="b-row">
					<view class="b-tit">下单时间</view>
					<view class="b-cont">
						<text class="s-text">{{timeFormat(orderData.created_at)}}</text>
					</view>
				</view>
			</view>
			<view class="box" v-if="orderData.status == 1">
				<radio-group @change="onRadioChange" v-for="(item, index) in payList" :key="index">
					<view class="b-row f-pay">
						<view class="b-tit">
							<view class="iconfont" :class="'icon-'+item.icon"></view>
							<view class="s-tit">{{item.name}}</view>
						</view>
						<view class="b-cont">
							<radio value="wechat" :checked="item.checked" color="#DA5650" />
						</view>
					</view>
				</radio-group>
			</view>
			<view class="navbar" v-if="submitIn || orderData.opt_actions.length > 0">
				<view class="b-order" @click="onOrderList" v-if="submitIn">我的订单</view>
				<view class="b-action">
					<view class="b-btn f-cancel" @click="onCancel"
						v-if="orderData.opt_actions.indexOf('can_cancel') !== -1">取消订单</view>
					<view class="b-btn f-pay" @click="onOncePay" v-if="orderData.opt_actions.indexOf('can_pay') !== -1">
						立即支付
					</view>
					<view class="b-btn f-apply" @click="onJumpRefund(null)"
						v-if="orderData.opt_actions.indexOf('can_refund') !== -1">申请退款
					</view>
					<view class="b-btn f-result" @click="onJumpRefund(null)"
						v-if="orderData.opt_actions.indexOf('refund_result') !== -1">
						退款结果
					</view>
					<view class="b-btn f-apply" @click="onJumpInvoice"
						v-if="orderData.opt_actions.indexOf('can_invoice') !== -1">申请开票</view>
					<view class="b-btn f-result" @click="onJumpInvoice"
						v-if="orderData.opt_actions.indexOf('invoice_result') !== -1">开票结果
					</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText" :top="300"></load-more>
		<!-- 首页 -->
		<app-home></app-home>
	</view>
</template>

<script>
	import {
		getOrderDetail,
		orderCancel,
		oncePay
	} from '@/service/api/order.js'
	import {
		requestPayment
	} from '@/service/request/main.js'
	import mixinsAuth from '@/mixins/auth.js'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				orderData: null,
				submitIn: false,
				payList: [{
					icon: 'pay-wechat',
					alias: 'wechat',
					name: '微信支付',
					checked: true
				}],
				payIndex: 0,
				loadingType: 1,
				loadingText: '',

			}
		},
		onLoad(options) {
			this.orderNo = options.order_no
			this.loadDetail()
			if (options.submit) {
				this.submitIn = true
			}
		},
		computed: {
			stateStyle() {
				return {
					'f-wait': this.orderData.status === 1,
					'f-done': this.orderData.status === 2,
					'f-cancel': this.orderData.status === 3,
				}
			},
			stateIcon() {
				return {
					'icon-wait': this.orderData.status === 1,
					'icon-done': this.orderData.status === 2,
					'icon-cancel': this.orderData.status === 3,
				}
			},
			leftAmount() {
				return (this.orderData.payment - this.orderData.balance).toFixed(2)
			}
		},
		methods: {
			loadDetail() {
				getOrderDetail(this.orderNo).then(([err, res]) => {
					console.log('getOrderDetail', err, res)
					if (!err) {
						this.loadingType = -1
						this.orderData = res
					} else {
						this.loadingType = 3
						this.loadingText = err.data.msg || '加载失败'
					}
				})
			},
			onRadioChange(e) {
				console.log('onRadioChange', e)
			},
			onOrderList() {
				uni.reLaunch({
					url: '/pages/order/list'
				})
			},
			onJumpHome() {
				uni.reLaunch({
					url: '/pages/home/index'
				})
			},
			onJumpRefund(orderItemNo) {
				let pageUrl = '/pages/order/refund?order_no=' + this.orderData.order_no
				if (orderItemNo) {
					pageUrl += '&order_item_no=' + orderItemNo
				}
				uni.navigateTo({
					url: pageUrl
				})
			},
			onJumpInvoice() {
				uni.navigateTo({
					url: '/pages/order/invoice?order_no=' + this.orderData.order_no
				})
			},
			onJumpGoodsDetail(goodsType, goodsId) {
				if (goodsType === 'course') {
					uni.navigateTo({
						url: '/pages/course/detail?cid=' + goodsId
					})
				} else if (goodsType === 'schedule') {
					uni.navigateTo({
						url: '/pages/schedule/detail?sid=' + goodsId
					})
				}
			},
			onCancel() {
				orderCancel(this.orderNo).then(([err, res]) => {
					console.log('orderCancel', err, res)
					if (!err) {
						this.$logic.showToast('取消成功')
						this.loadDetail()
					}
				})
			},
			onOncePay() {
				const payAlias = this.payList[this.payIndex].alias
				requestPayment(this.orderNo, payAlias)
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {
		padding-bottom: 150upx;
	}

	.state-box {
		height: 180upx;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #fff;

		&.f-wait {
			color: #FFC300;
		}

		&.f-done {
			color: #DA5650;
		}

		&.f-cancel {
			color: #666;
		}

		.iconfont {
			font-size: 64upx;
		}

		.s-tit {
			margin-left: 20upx;
			font-size: 50upx;
			font-weight: bold;
		}
	}

	.gift-head {
		margin-top: 30upx;
		padding: 20upx 30upx;
		font-size: 28upx;
		color: #333;
		background: #fff;
		border-bottom: 2upx solid #eee;
	}

	.gift-list {
		background: #fff;

		.gift-item {
			padding: 30upx;
			border-bottom: 2upx solid #eee;
			display: flex;
			align-items: center;

			.b-left {
				width: 160upx;
				height: 160upx;

				image {
					width: 160upx;
					height: 160upx;
				}
			}

			.b-main {
				margin-left: 30upx;
				flex: 1;
				display: flex;
				flex-direction: column;

				.b-title {
					font-size: 28upx;
					color: #333;
				}


				.b-spec {
					margin-top: 20upx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.s-value {
						font-size: 24upx;
						color: #666;
					}
				}

				.b-fund {
					margin-top: 20upx;
					display: flex;
					align-items: center;

					.b-amount {
						display: flex;
						align-items: baseline;

						.s-count {
							font-size: 26upx;
							color: #666;
						}

						.s-worth {
							margin-left: 20upx;
							font-size: 26upx;
							color: #999;
						}

						.s-num {
							margin-left: 30upx;
							font-size: 28upx;
							color: #DA5650;
						}
					}

					.b-action {
						margin-left: 30upx;
						display: flex;
						align-items: center;

						.s-btn {
							background: #DA5650;
							height: 50upx;
							border-radius: 25upx;
							padding: 0 20upx;
							line-height: 50upx;
							font-size: 24upx;
							color: #fff;

							&.f-apply {
								color: #666;
								background: none;
								border: 2upx solid #eee;
							}

							&.f-result {
								color: #fff;
								background: #FFC300;
							}
						}
					}
				}
			}
		}

		.gift-note {
			padding: 20upx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.b-tit {
				width: 200upx;
				font-size: 28upx;
				color: #333;
			}

			.b-cont {
				width: 480upx;
				font-size: 28upx;
				color: #333;
			}
		}
	}

	.box {
		margin-top: 30upx;
		background: #fff;
		padding: 15upx 0;

		.b-row {
			padding: 15upx 30upx;
			display: flex;

			// justify-content: space-between;
			&.f-link {
				align-items: center;
			}

			&.f-pay {
				align-items: center;
				justify-content: space-between;

				.b-tit {
					width: 320upx;
					display: flex;
					align-items: center;

					.iconfont {
						font-size: 50upx;
						color: #6CB37A;
					}

					.s-tit {
						margin-left: 10upx;
						font-size: 28upx;
					}
				}

				.b-cont {
					width: auto;

					radio {
						transform: scale(0.8)
					}
				}
			}

			.b-tit {
				font-size: 28upx;
				width: 180upx;
				color: #666;
			}

			.b-cont {
				width: 510upx;
				display: flex;
				align-items: center;
				font-size: 28upx;
				color: #333;
			}

			.s-copy {
				margin-left: 20upx;
				font-size: 24upx;
				color: #FFC300;
			}

			.s-province,
			.s-city,
			.s-area {
				margin-right: 10upx;
			}

			.s-phone {
				margin-left: 20upx;
			}

			.s-text {
				color: #333;
				font-weight: bold;
			}

			.s-btn {
				padding: 0 30upx;
				background: #FFC300;
				color: #fff;
				font-size: 24upx;
				height: 60upx;
				line-height: 60upx;
				border-radius: 30upx;
			}
		}
	}

	.navbar {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 100upx;
		background: #fff;
		border-top: 1upx solid #f6f6f6;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-top: 1upx solid #eee;

		.b-order {
			margin-left: 20upx;
			padding: 0 30upx;
			height: 60upx;
			line-height: 60upx;
			border-radius: 30upx;
			border: 2upx solid #DA5650;
			font-size: 26upx;
			color: #DA5650;
			letter-spacing: 4upx;
		}

		.b-action {
			margin: 0 30upx;
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: flex-end;

			.b-btn {
				margin-left: 20upx;
				background: #DA5650;
				height: 60upx;
				border-radius: 30upx;
				padding: 0 30upx;
				line-height: 60upx;
				font-size: 26upx;
				color: #fff;
				letter-spacing: 4upx;

				&.f-pay {
					color: #fff;
					background: #FFC300;
				}

				&.f-cancel {
					color: #666;
					background: none;
					border: 2upx solid #eee;
				}

				&.f-apply {
					color: #666;
					background: none;
					border: 2upx solid #eee;
				}

				&.f-result {
					color: #fff;
					background: #FFC300;
				}
			}
		}
	}
</style>
